package sessionBeans;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import sessionBeans.Interfaces.IGestCrudRemote;

/**
 * Session Bean implementation class GestCrud
 */
@Stateless
public abstract class GestCrud<T> implements IGestCrudRemote<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@PersistenceContext
	protected EntityManager em;

	protected Class<T> entityClass;

	/**
	 * Default constructor.
	 */
	public GestCrud() {
		// TODO Auto-generated constructor stub
	}

	public GestCrud(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	@Override
	public void create(T entity) {
		// TODO Auto-generated method stub
		em.persist(entity);
	}

	@Override
	public T find(long id) {
		// TODO Auto-generated method stub
		return (T) em.find(this.entityClass, id);
	}

	@Override
	public void update(T entity) {
		em.merge(entity);
	}

	@Override
	public void delete(T entity) {
		if (em.contains(entity)) {
			em.remove(entity);
		}
		else {
			T temp = em.merge(entity);
			em.remove(temp);
		}
	}

}
